package com.chunyu.ask.controller.jingyan;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.chunyu.ask.bean.Article;
import com.chunyu.ask.bean.Channel;
import com.chunyu.ask.bean.Tag;
import com.chunyu.ask.service.ArticleService;
import com.chunyu.ask.service.ChannelService;
import com.chunyu.ask.util.PageUtils;
import com.chunyu.ask.util.RespMsg;

/**
 * 描        述：健康经验
 * 创建时间：2016-8-22
 * @author Jibaole
 */
@Controller
@RequestMapping("/jingyan")
public class JingYanController {
	@Autowired
	private ArticleService articleService;
	@Autowired
	private ChannelService channelService;
	//得到当前频道下，所有频道id
	private StringBuffer sbfChannelId = new StringBuffer();
	
	
	/**
	 * 健康经验-首页   第一级
	 * @param urlName
	 * @param channelStrId
	 * @return
	 */
	@RequestMapping("/{urllevel1}/{channelStrId}")
	public @ResponseBody ModelAndView Jkjy(@PathVariable String urllevel1,@PathVariable String channelStrId) {
		ModelAndView view = new ModelAndView(PageUtils.JINGYAN_LIST);
		Map<String, Object> paramMap = new HashMap<String, Object>();
			try {
			getViewDate(urllevel1, channelStrId, view, paramMap);
			view.addObject("urlName",urllevel1);
			} catch (Exception e) {
			e.printStackTrace();
		}
		return view;
	}
	
	/**
	 * 经验第二级频道&&列表
	 * @param urllevel1
	 * @param urllevel2
	 * @param channelStrId
	 * @param req
	 * @return
	 */
	@RequestMapping("/{urllevel1}/{urllevel2}/{channelStrId}")
	public @ResponseBody ModelAndView JkjyLevel2(@PathVariable String urllevel1,@PathVariable String urllevel2,@PathVariable String channelStrId) {
		ModelAndView view = new ModelAndView(PageUtils.JINGYAN_LIST);
		Map<String, Object> paramMap = new HashMap<String, Object>();
			try {
			getViewDate(urllevel1, channelStrId, view, paramMap);
			view.addObject("urlName",urllevel1+"/"+urllevel2);
			} catch (Exception e) {
			e.printStackTrace();
		}
		return view;
	}
	
	/**
	 * 经验第三级频道&&列表
	 * @param urllevel1
	 * @param urllevel2
	 * @param urllevel3
	 * @param channelStrId
	 * @param req
	 * @return
	 */
	@RequestMapping("/{urllevel1}/{urllevel2}/{urllevel3}/{channelStrId}")
	public @ResponseBody ModelAndView JkjyLevel3(@PathVariable String urllevel1,@PathVariable String urllevel2,
			@PathVariable String urllevel3,@PathVariable String channelStrId) {
		ModelAndView view = new ModelAndView(PageUtils.JINGYAN_LIST);
		Map<String, Object> paramMap = new HashMap<String, Object>();
			try {
				getViewDate(urllevel1, channelStrId, view, paramMap);
			    view.addObject("urlName",urllevel1+"/"+urllevel2+"/"+urllevel3);
			
			} catch (Exception e) {
			e.printStackTrace();
		}
		return view;
	}
	
	/**
	 * 经验第四级频道&&列表
	 * @param urllevel1
	 * @param urllevel2
	 * @param urllevel3
	 * @param channelStrId
	 * @param req
	 * @return
	 */
	@RequestMapping("/{urllevel1}/{urllevel2}/{urllevel3}/{urllevel4}/{channelStrId}")
	public @ResponseBody ModelAndView JkjyLevel4(@PathVariable String urllevel1,@PathVariable String urllevel2,
			@PathVariable String urllevel3,@PathVariable String urllevel4,@PathVariable String channelStrId) {
		ModelAndView view = new ModelAndView(PageUtils.JINGYAN_LIST);
		Map<String, Object> paramMap = new HashMap<String, Object>();
			try {
				getViewDate(urllevel1, channelStrId, view, paramMap);
			    view.addObject("urlName",urllevel1+"/"+urllevel2+"/"+urllevel3+"/"+urllevel4);
			} catch (Exception e) {
			e.printStackTrace();
		}
		return view;
	}
	/**
	 * 加载更多列表数据
	 * @param channelId
	 * @param req
	 * @return
	 */
	 @RequestMapping("/getArticleList/{channelId}/")
		public @ResponseBody
		RespMsg getArticleList(@PathVariable String channelId,HttpServletRequest req) {
			RespMsg result = new RespMsg();
			Map<String, Object> paramMap = new HashMap<String, Object>();
			String page = req.getParameter("page");
			try {
				sbfChannelId = new StringBuffer(channelId+",");
				//得到当前频道下，所有频道id
				getAllChannelId(channelId+"");
				
				if(!StringUtils.isEmpty(sbfChannelId)){
					String channelIds = sbfChannelId.substring(0, sbfChannelId.length()-1);
					//对应咨询文章
					paramMap.put("channelIds", channelIds);
					if(!StringUtils.isEmpty(page)){
						paramMap.put("firstResult", Integer.parseInt(page)*20);
					}
					List<Article> articleList = articleService.getList(paramMap);
					result.resultObj = articleList;
				}
				result.status = true;
			} catch (Exception e) {
				result.status = false;
				result.msg="加载文章列表异常";
				e.printStackTrace();
			}
			return result;
		}
	 
	 /**
     * 健康经验内容页
     * @param id
     * @return
     */
	@RequestMapping("/page/{id}")
	public ModelAndView page(@PathVariable int id) {
		ModelAndView view = new ModelAndView(PageUtils.JINGYAN_DETAIL);
		List<String> contentlist = new ArrayList<String>();
		try {
			Article article = articleService.getById(id);
			if (null != article && !StringUtils.isEmpty(article.getContent())) {
				JSONObject jsonObj = new JSONObject(article.getContent());
				if (null != jsonObj && jsonObj.length() > 0) {
					String remind = (String) jsonObj.get("remind");
					String abst = (String) jsonObj.get("abst");
					JSONArray steps = jsonObj.getJSONArray("steps");
					view.addObject("remind", remind);
					view.addObject("abst", abst);
					if (null != steps && steps.length() > 0) {
						for (int i = 0; i < steps.length(); i++) {
							JSONObject obj = steps.getJSONObject(i);
							contentlist.add((String) obj.get("step_content"));
						}
					}
					view.addObject("contentlist", contentlist);
				}
			}
			view.addObject("article", article);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return view;
	}
	
	/**
	 * 封装：健康咨询页面数据
	 * @param urllevel1
	 * @param channelStrId
	 * @param view
	 * @param paramMap
	 */
	private void getViewDate(String urllevel1, String channelStrId,
			ModelAndView view, Map<String, Object> paramMap) {
		int id=0;
		paramMap.put("strId", channelStrId);
		//频道id
		Channel obj=channelService.getChannel(paramMap);
		if(obj != null){
			id = obj.getId();
			view.addObject("channelId",id);
			view.addObject("tagName",obj.getName());
		}
		
		paramMap.clear();
		paramMap.put("parentId", id);
		//标签列表
		paramMap.put("firstResult", 0);
		paramMap.put("maxResult", Integer.MAX_VALUE);
		List<Channel> channelList = channelService.getList(paramMap);
		view.addObject("channelList",channelList);
		
		sbfChannelId = new StringBuffer(id+",");
		//得到当前频道下，所有频道id
		getAllChannelId(id+"");
		if(!StringUtils.isEmpty(sbfChannelId)){
			String channelIds = sbfChannelId.substring(0, sbfChannelId.length()-1);
			//对应咨询文章
			paramMap.clear();
			paramMap.put("channelIds", channelIds);
			List<Article> articleList = articleService.getList(paramMap);
			view.addObject("articleList",articleList);
		}
	}
	
	/**
	 * 得到顶级频道下,所有频道id
	 * @return
	 */
	
	public String getAllChannelId(String  parentId){
		Map<String, Object> paramMap = new HashMap<String, Object>();
			if(!StringUtils.isEmpty(parentId)){
				paramMap.put("parentIds", parentId);
				//标签列表
				paramMap.put("firstResult", 0);
				paramMap.put("maxResult", Integer.MAX_VALUE);
				List<Channel> channelList = channelService.getList(paramMap);
			
			if(!CollectionUtils.isEmpty(channelList)){
				StringBuffer sbf = new StringBuffer();
				for(Channel obj:channelList){
					sbf.append(obj.getId()+",");
					sbfChannelId.append(obj.getId()+",");
				}
				if(!StringUtils.isEmpty(sbf)){
					String parentIds =  sbf.substring(0, sbf.length()-1);
					getAllChannelId(parentIds);
				}
			}
		}
		return null;
	}
	
	/**
	 * 获取：健康经验tag标签列表
	 * @return
	 */
	@RequestMapping("/getTagList")
	public @ResponseBody RespMsg getTagList() {
		RespMsg result = new RespMsg();
		List<Tag> tagList = new ArrayList<Tag>();
		try {
			
			tagList.add(new Tag("/jingyan/nxjb/aowmao3qyv/", "男性疾病"));
			tagList.add(new Tag("/jingyan/nxjb/1kwvmhsksk/", "女性疾病"));
			tagList.add(new Tag("/jingyan/lxjk/t8zqaosky4wvmhsksk/", "两性健康"));
			tagList.add(new Tag("/jingyan/myye/aowmao80we/", "母婴育儿"));
			tagList.add(new Tag("/jingyan/nkjb/1kwv80sksk/", "内科疾病"));
			tagList.add(new Tag("/jingyan/wkjb/1kwv9ysksk/", "外科疾病"));
			tagList.add(new Tag("/jingyan/wgjb/nxwmrfsksk/", "五官疾病"));
			tagList.add(new Tag("/jingyan/wgjb/nxwmrfsksk/", "五官疾病"));
			tagList.add(new Tag("/jingyan/pfjb/1kwmmhsksk/", "皮肤疾病"));
			tagList.add(new Tag("null", "家庭用药"));
			
			result.resultObj=tagList;
			result.status = true;
		} catch (Exception e) {
			result.status = false;
			result.msg="加载经验频道列表异常";
			e.printStackTrace();
		}
		return result;
	}
}
